home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GXDRAW.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  14KB  |  445 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxdraw.c */
  20. /* Primitive drawing routines for Ghostscript imaging library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzdevice.h"            /* requires gsstate.h */
  29. #include "gzcolor.h"            /* requires gxdevice.h */
  30.  
  31. /* Fill a rectangle. */
  32. int
  33. gz_fill_rectangle(int x, int y, int w, int h, const gx_device_color *pdevc,
  34.   gs_state *pgs)
  35. {    gx_device *dev = pgs->device->info;
  36.     if_debug7('v', "[v]x=%d y=%d w=%d h=%d  c1=%ld c2=%ld htl=%d\n",
  37.           x, y, w, h, (long)pdevc->color1, (long)pdevc->color2,
  38.           (long)pdevc->halftone_level);
  39.     return gz_fill_rectangle_open(dev, x, y, w, h, dev->procs->fill_rectangle, dev->procs->tile_rectangle, pdevc, pgs);
  40. }
  41.  
  42. /*
  43.  * Auxiliary procedures for computing a * b / c and a * b % c
  44.  * when a, b, and c are all non-negative,
  45.  * b < c, and a * b exceeds (or might exceed) the capacity of a long.
  46.  * It's really annoying that C doesn't provide any way to get at
  47.  * the double-length multiply/divide instructions that
  48.  * the machine undoubtedly provides....
  49.  *
  50.  * Note that these routines are exported for the benefit of gxfill.c.
  51.  */
  52.  
  53. fixed
  54. fixed_mult_quo(fixed a, fixed b, fixed c)
  55. {    return (fixed)floor((double)a * b / c);
  56. }
  57. fixed
  58. fixed_mult_rem(fixed a, fixed b, fixed c)
  59. {    double prod = (double)a * b;
  60.     return (fixed)(prod - floor(prod / c) * c);
  61. }
  62.  
  63. /* Fill a trapezoid.  Requires: wt >= 0, wb >= 0, h >= 0. */
  64. /* Note that the arguments are fixeds, not ints! */
  65. /* This is derived from Paul Haeberli's floating point algorithm. */
  66. typedef struct trap_line_s {
  67.     int di; fixed df;        /* dx/dy ratio */
  68.     fixed ldi, ldf;            /* increment per scan line */
  69.     fixed x, xf;            /* current value */
  70. } trap_line;
  71. int
  72. gz_fill_trapezoid_fixed(fixed fx0, fixed fw0, fixed fy0,
  73.   fixed fx1, fixed fw1, fixed fh, int swap_axes,
  74.   const gx_device_color *pdevc, gs_state *pgs)
  75. {    const fixed ymin = fixed_rounded(fy0) + fixed_half;
  76.     const fixed ymax = fixed_rounded(fy0 + fh);
  77.     int iy = fixed2int_var(ymin);
  78.     const int iy1 = fixed2int_var(ymax);
  79.     if ( iy >= iy1 ) return 0;    /* no scan lines to sample */
  80.    {    trap_line l, r;
  81.     int rxl, rxr, ry;
  82.     const fixed dxl = fx1 - fx0;
  83.     const fixed dxr = dxl + fw1 - fw0;
  84.     const fixed yline = ymin - fy0;    /* partial pixel offset to */
  85.                     /* first line to sample */
  86.     int fill_direct = color_is_pure(pdevc);
  87.     gx_color_index cindex;
  88.     gx_device *dev;
  89.     dev_proc_fill_rectangle((*fill_rect));
  90.     int code;
  91.     
  92.     if_debug2('z', "[z]y=[%d,%d]\n", iy, iy1);
  93.  
  94.     if ( fill_direct )
  95.       cindex = pdevc->color1,
  96.       dev = pgs->device->info,
  97.       fill_rect = dev->procs->fill_rectangle;
  98.     return_if_interrupt();
  99.     r.x = (l.x = fx0 + fixed_half) + fw0;
  100.     ry = iy;
  101.  
  102.     /* If the rounded X values are the same on both sides, */
  103.     /* we can save ourselves a *lot* of work. */
  104.     if ( fixed_floor(l.x) == fixed_rounded(fx1) &&
  105.          fixed_floor(r.x) == fixed_rounded(fx1 + fw1)
  106.        )
  107.     {    rxl = fixed2int_var(l.x);
  108.         rxr = fixed2int_var(r.x);
  109.         iy = iy1;
  110.         if_debug2('z', "[z]rectangle, x=[%d,%d]\n", rxl, rxr);
  111.         goto last;
  112.     }
  113.  
  114. #define fill_trap_rect(x,y,w,h)\
  115.   (fill_direct ?\
  116.     (swap_axes ? (*fill_rect)(dev, y, x, h, w, cindex) :\
  117.      (*fill_rect)(dev, x, y, w, h, cindex)) :\
  118.    swap_axes ? gz_fill_rectangle(y, x, h, w, pdevc, pgs) :\
  119.    gz_fill_rectangle(x, y, w, h, pdevc, pgs))
  120.  
  121.     /* Compute the dx/dy ratios. */
  122.     /* dx# = dx#i + (dx#f / fh). */
  123. #define compute_dx(tl, d)\
  124.   if ( d >= 0 )\
  125.    { if ( d < fh ) tl.di = 0, tl.df = d;\
  126.      else tl.di = (int)(d / fh), tl.df = d - tl.di * fh, tl.x += yline * tl.di;\
  127.    }\
  128.   else\
  129.    { if ( (tl.df = d + fh) >= 0 /* d >= -fh */ ) tl.di = -1, tl.x -= yline;\
  130.      else tl.di = (int)-((fh - 1 - d) / fh), tl.df = d - tl.di * fh, tl.x += yline * tl.di;\
  131.    }
  132.  
  133.     /* Compute the x offsets at the first scan line to sample. */
  134.     /* We need to be careful in computing yline * dx#f {/,%} fh */
  135.     /* because the multiplication may overflow.  We know that */
  136.     /* all the quantities involved are non-negative, and that */
  137.     /* yline is less than 1 (as a fixed, of course); this gives us */
  138.     /* a cheap conservative check for overflow in the multiplication. */
  139. #define ymult_limit (max_fixed / fixed_1)
  140. #define ymult_quo(yl, dxxf)\
  141.   (dxxf < ymult_limit ? yl * dxxf / fh : fixed_mult_quo(yl, dxxf, fh))
  142. #define ymult_rem(yl, dxxf)\
  143.   (dxxf < ymult_limit ? yl * dxxf % fh : fixed_mult_rem(yl, dxxf, fh))
  144.  
  145.     /* It's worth checking for dxl == dxr, since this is the case */
  146.     /* for parallelograms (including stroked lines). */
  147.     compute_dx(l, dxl);
  148.     if ( dxr == dxl )
  149.        {    fixed fx = ymult_quo(yline, l.df);
  150.         l.x += fx;
  151.         if ( l.di == 0 )
  152.             r.di = 0, r.df = l.df;
  153.         else            /* too hard to do adjustments right */
  154.             compute_dx(r, dxr);
  155.         r.x += fx;
  156.        }
  157.     else
  158.        {    l.x += ymult_quo(yline, l.df);
  159.         compute_dx(r, dxr);
  160.         r.x += ymult_quo(yline, r.df);
  161.        }
  162.     rxl = fixed2int_var(l.x);
  163.     rxr = fixed2int_var(r.x);
  164.  
  165.     /* Compute one line's worth of dx/dy. */
  166.     /* dx# * fixed_1 = ld#i + (ld#f / fh). */
  167.     /* We don't have to bother with this if */
  168.     /* we're only sampling a single scan line. */
  169.     if ( iy1 - iy == 1 )
  170.        {    iy++;
  171.         goto last;
  172.        }
  173. #define compute_ldx(tl)\
  174.   if ( tl.df < ymult_limit )\
  175.     tl.ldi = int2fixed(tl.di) + int2fixed(tl.df) / fh,\
  176.     tl.ldf = int2fixed(tl.df) % fh,\
  177.     tl.xf = yline * tl.df % fh - fh;\
  178.   else\
  179.     tl.ldi = int2fixed(tl.di) + fixed_mult_quo(fixed_1, tl.df, fh),\
  180.     tl.ldf = fixed_mult_rem(fixed_1, tl.df, fh),\
  181.     tl.xf = fixed_mult_rem(yline, tl.df, fh) - fh
  182.     compute_ldx(l);
  183.     if ( dxr == dxl )
  184.         r.ldi = l.ldi, r.ldf = l.ldf, r.xf = l.xf;
  185.     else
  186.        {    compute_ldx(r);
  187.        }
  188. #undef compute_ldx
  189.  
  190.     while ( ++iy != iy1 )
  191.        {    int ixl, ixr;
  192. #define step_line(tl)\
  193.   tl.x += tl.ldi;\
  194.   if ( (tl.xf += tl.ldf) >= 0 ) tl.xf -= fh, tl.x++;
  195.         step_line(l);
  196.         step_line(r);
  197. #undef step_line
  198.         ixl = fixed2int_var(l.x);
  199.         ixr = fixed2int_var(r.x);
  200.         if ( ixl != rxl || ixr != rxr )
  201.            {    code = fill_trap_rect(rxl, ry, rxr - rxl, iy - ry);
  202.             if ( code < 0 ) goto xit;
  203.             rxl = ixl, rxr = ixr, ry = iy;
  204.            }    
  205.        }
  206. last:    code = fill_trap_rect(rxl, ry, rxr - rxl, iy - ry);
  207. xit:    if ( code < 0 && fill_direct )
  208.         return_error(code);
  209.     return_if_interrupt();
  210.     return code;
  211.    }
  212. }
  213.  
  214. /* Fill a parallelogram whose points are p, p+a, p+b, and p+a+b. */
  215. /* We should swap axes to get best accuracy, but we don't. */
  216. int
  217. gz_fill_pgram_fixed(fixed px, fixed py, fixed ax, fixed ay,
  218.   fixed bx, fixed by, const gx_device_color *pdevc, gs_state *pgs)
  219. {    fixed t;
  220.     fixed dy, qx, dx, wx, pax, qax;
  221.     int code;
  222. #define swap(r, s) (t = r, r = s, s = t)
  223.     /* Reorder the points so that 0 <= ay <= by. */
  224.     if ( ay < 0 ) px += ax, py += ay, ax = -ax, ay = -ay;
  225.     if ( by < 0 ) px += bx, py += by, bx = -bx, by = -by;
  226.     if ( ay > by ) swap(ax, bx), swap(ay, by);
  227.     if ( by == 0 ) return 0;    /* degenerate (line) */
  228.     qx = px + ax + bx;
  229.     /* Compute the distance from p to the point on the line (p, p+b) */
  230.     /* whose Y coordinate is equal to ay. */
  231.     dx = ( ((bx < 0 ? -bx : bx) | ay) < 1L << (size_of(fixed) * 4 - 1) ?
  232.         bx * ay / by :
  233.         (fixed)((double)bx * ay / by)
  234.          );
  235.     if ( dx < ax ) pax = px + dx, qax = qx - ax, wx = ax - dx;
  236.     else pax = px + ax, qax = qx - dx, wx = dx - ax;
  237. #define rounded_same(p, a) /* fixed_rounded(p) == fixed_rounded(p + a) */\
  238.   (fixed_fraction((p) + fixed_half) + (a) < fixed_1) /* know a >= 0 */
  239.     if ( !rounded_same(py, ay) )
  240.        {    code = gz_fill_trapezoid_fixed(px, fixed_0, py, pax, wx, ay,
  241.                            0, pdevc, pgs);
  242.         if ( code < 0 ) return code;
  243.        }
  244.     py += ay;
  245.     dy = by - ay;
  246.     if ( !rounded_same(py, dy) )
  247.        {    code = gz_fill_trapezoid_fixed(pax, wx, py, qax, wx, dy,
  248.                            0, pdevc, pgs);
  249.         if ( code < 0 ) return code;
  250.        }
  251.     py += dy;
  252.     if ( !rounded_same(py, ay) )
  253.         return gz_fill_trapezoid_fixed(qax, wx, py, qx, fixed_0, ay,
  254.                            0, pdevc, pgs);
  255. #undef rounded_same
  256. #undef swap
  257.     return 0;
  258. }
  259.  
  260. /* Default implementation of tile_rectangle */
  261. int
  262. gx_default_tile_rectangle(gx_device *dev, register const gx_bitmap *tile,
  263.   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  264.   int px, int py)
  265. {    /* Fill the rectangle in chunks */
  266.     int width = tile->size.x;
  267.     int height = tile->size.y;
  268.     int raster = tile->raster;
  269.     int rwidth = tile->rep_width;
  270.     int irx = ((rwidth & (rwidth - 1)) == 0 ?    /* power of 2 */
  271.         (x + px) & (rwidth - 1) :
  272.         (x + px) % rwidth);
  273.     int ry = (y + py) % tile->rep_height;
  274.     int icw = width - irx;
  275.     int ch = height - ry;
  276.     byte *row = tile->data + ry * raster;
  277. #define d_proc_mono (dev->procs->copy_mono)
  278.     dev_proc_copy_mono((*proc_mono));
  279. #define d_proc_color (dev->procs->copy_color)
  280.     dev_proc_copy_color((*proc_color));
  281. #define d_color_halftone\
  282.         (color0 == gx_no_color_index && color1 == gx_no_color_index)
  283.     int color_halftone;
  284. #define get_color_info()\
  285.   if ( (color_halftone = d_color_halftone) ) proc_color = d_proc_color;\
  286.   else proc_mono = d_proc_mono
  287.     int code;
  288. #ifdef DEBUG
  289. if ( gs_debug['t'] )
  290.    {    int ptx, pty;
  291.     const byte *ptp = tile->data;
  292.     dprintf3("[t]tile %dx%d raster=%d;",
  293.         tile->size.x, tile->size.y, tile->raster);
  294.     dprintf6(" x,y=%d,%d w,h=%d,%d p=%d,%d\n",
  295.         x, y, w, h, px, py);
  296.     for ( pty = 0; pty < tile->size.y; pty++ )
  297.        {    dprintf("   ");
  298.         for ( ptx = 0; ptx < tile->raster; ptx++ )
  299.             dprintf1("%3x", *ptp++);
  300.        }
  301.     dputc('\n');
  302.    }
  303. #endif
  304. #define real_copy_tile(srcx, tx, ty, tw, th)\
  305.   code =\
  306.     (color_halftone ?\
  307.      (*proc_color)(dev, row, srcx, raster, gx_no_bitmap_id, tx, ty, tw, th) :\
  308.      (*proc_mono)(dev, row, srcx, raster, gx_no_bitmap_id, tx, ty, tw, th, color0, color1));\
  309.   if ( code < 0 ) return_error(code);\
  310.   return_if_interrupt()
  311. #ifdef DEBUG
  312. #define copy_tile(sx, tx, ty, tw, th)\
  313.   if ( gs_debug['t'] )\
  314.     dprintf5("   copy sx=%d x=%d y=%d w=%d h=%d\n",\
  315.          sx, tx, ty, tw, th);\
  316.   real_copy_tile(sx, tx, ty, tw, th)
  317. #else
  318. #define copy_tile(sx, tx, ty, tw, th)\
  319.   real_copy_tile(sx, tx, ty, tw, th)
  320. #endif
  321.     if ( icw >= w )
  322.        {    /* Narrow operation */
  323.         int ey, fey, cy;
  324.         if ( ch >= h )
  325.            {    /* Just one (partial) tile to transfer. */
  326. #define color_halftone d_color_halftone
  327. #define proc_color d_proc_color
  328. #define proc_mono d_proc_mono
  329.             copy_tile(irx, x, y, w, h);
  330. #undef proc_mono
  331. #undef proc_color
  332. #undef color_halftone
  333.             return 0;
  334.            }
  335.         get_color_info();
  336.         ey = y + h;
  337.         fey = ey - height;
  338.         copy_tile(irx, x, y, w, ch);
  339.         cy = y + ch;
  340.         row = tile->data;
  341.         do
  342.            {    ch = (cy > fey ? ey - cy : height);
  343.             copy_tile(irx, x, cy, w, ch);
  344.            }
  345.         while ( (cy += ch) < ey );
  346.         return 0;
  347.        }
  348.     get_color_info();
  349.     if ( ch >= h )
  350.        {    /* Shallow operation */
  351.         int ex = x + w;
  352.         int fex = ex - width;
  353.         int cx = x + icw;
  354.         copy_tile(irx, x, y, icw, h);
  355.         while ( cx <= fex )
  356.            {    copy_tile(0, cx, y, width, h);
  357.             cx += width;
  358.            }
  359.         if ( cx < ex )
  360.            {    copy_tile(0, cx, y, ex - cx, h);
  361.            }
  362.        }
  363.     else
  364.        {    /* Full operation */
  365.         int ex = x + w, ey = y + h;
  366.         int fex = ex - width, fey = ey - height;
  367.         int cx, cy;
  368.         for ( cy = y; ; )
  369.            {    copy_tile(irx, x, cy, icw, ch);
  370.             cx = x + icw;
  371.             while ( cx <= fex )
  372.                {    copy_tile(0, cx, cy, width, ch);
  373.                 cx += width;
  374.                }
  375.             if ( cx < ex )
  376.                {    copy_tile(0, cx, cy, ex - cx, ch);
  377.                }
  378.             if ( (cy += ch) >= ey ) break;
  379.             ch = (cy > fey ? ey - cy : height);
  380.             row = tile->data;
  381.            }
  382.        }
  383. #undef copy_tile
  384. #undef real_copy_tile
  385.     return 0;
  386. }
  387.  
  388. /* Draw a one-pixel-wide line. */
  389. int
  390. gz_draw_line_fixed(fixed ixf, fixed iyf, fixed itoxf, fixed itoyf,
  391.   const gx_device_color *pdevc, gs_state *pgs)
  392. {    int ix = fixed2int_var(ixf);
  393.     int iy = fixed2int_var(iyf);
  394.     int itox = fixed2int_var(itoxf);
  395.     int itoy = fixed2int_var(itoyf);
  396.     gx_device *dev;
  397.     return_if_interrupt();
  398.     if ( itoy == iy )        /* horizontal line */
  399.       { return (ix <= itox ?
  400.             gz_fill_rectangle(ix, iy, itox - ix + 1, 1, pdevc, pgs) :
  401.             gz_fill_rectangle(itox, iy, ix - itox + 1, 1, pdevc, pgs)
  402.             );
  403.       }
  404.     if ( itox == ix )        /* vertical line */
  405.       { return (iy <= itoy ?
  406.             gz_fill_rectangle(ix, iy, 1, itoy - iy + 1, pdevc, pgs) :
  407.             gz_fill_rectangle(ix, itoy, 1, iy - itoy + 1, pdevc, pgs)
  408.             );
  409.       }
  410.     if ( color_is_pure(pdevc) &&
  411.         (dev = pgs->device->info,
  412.          (*dev->procs->draw_line)(dev, ix, iy, itox, itoy,
  413.                       pdevc->color1)) >= 0 )
  414.       return 0;
  415.     { fixed h = itoyf - iyf;
  416.       fixed w = itoxf - ixf;
  417.       fixed tf;
  418. #define fswap(a, b) tf = a, a = b, b = tf
  419.       if ( (w < 0 ? -w : w) <= (h < 0 ? -h : h) )
  420.         { if ( h < 0 )
  421.         fswap(ixf, itoxf), fswap(iyf, itoyf),
  422.         h = -h;
  423.           return gz_fill_trapezoid_fixed(ixf - fixed_half, fixed_1, iyf,
  424.                          itoxf - fixed_half, fixed_1, h,
  425.                          0, pdevc, pgs);
  426.         }
  427.       else
  428.         { if ( w < 0 )
  429.         fswap(ixf, itoxf), fswap(iyf, itoyf),
  430.         w = -w;
  431.           return gz_fill_trapezoid_fixed(iyf - fixed_half, fixed_1, ixf,
  432.                          itoyf - fixed_half, fixed_1, w,
  433.                          1, pdevc, pgs);
  434.         }
  435. #undef fswap
  436.     }
  437. }
  438.  
  439. /* A stub to force use of the standard procedure. */
  440. int
  441. gx_default_draw_line(gx_device *dev,
  442.   int x0, int y0, int x1, int y1, gx_color_index color)
  443. {    return -1;
  444. }
  445.